package com.forum.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.forum.annotation.IgnoreAuth;
import com.forum.entity.FavoritetabEntity;
import com.forum.entity.view.FavoritetabView;
import com.forum.service.FavoritetabService;
import com.forum.utils.MPUtil;
import com.forum.utils.PageUtils;
import com.forum.utils.R;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 收藏表 前端控制器
 * </p>
 *
 * @author 张继成
 * @since 2023-12-31
 */
@RestController
@RequestMapping("/storeup")
@Api(tags = "收藏表业务控制")
public class FavoritetabController {
    @Autowired
    private FavoritetabService storeupService;

    /**
     * 后端列表
     */
    @RequestMapping("/page")
    public R page(@RequestParam Map<String, Object> params, FavoritetabEntity storeup,
                  HttpServletRequest request){
        if(!request.getSession().getAttribute("role").toString().equals("管理员")) {
            storeup.setUserid((Long)request.getSession().getAttribute("userId"));
        }
        EntityWrapper<FavoritetabEntity> ew = new EntityWrapper<FavoritetabEntity>();

        PageUtils page = storeupService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, storeup), params), params));

        return R.ok().put("data", page);
    }

    /**
     * 前端列表
     */
    @IgnoreAuth
    @RequestMapping("/list")
    @SuppressWarnings("unchecked")
    public R list(@RequestParam Map<String, Object> params,FavoritetabEntity storeup,
                  HttpServletRequest request){
        EntityWrapper<FavoritetabEntity> ew = new EntityWrapper<FavoritetabEntity>();

        PageUtils page = storeupService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, storeup), params), params));
        return R.ok().put("data", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/lists")
    @SuppressWarnings("unchecked")
    public R list( FavoritetabEntity storeup){
        EntityWrapper<FavoritetabEntity> ew = new EntityWrapper<FavoritetabEntity>();
        ew.allEq(MPUtil.allEQMapPre( storeup, "storeup"));
        return R.ok().put("data", storeupService.selectListView(ew));
    }

    /**
     * 查询
     */
    @RequestMapping("/query")
    @SuppressWarnings("unchecked")
    public R query(FavoritetabEntity storeup){
        EntityWrapper< FavoritetabEntity> ew = new EntityWrapper< FavoritetabEntity>();
        ew.allEq(MPUtil.allEQMapPre( storeup, "storeup"));
        FavoritetabView storeupView =  storeupService.selectView(ew);
        return R.ok("查询收藏表成功").put("data", storeupView);
    }

    /**
     * 后端详情
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        FavoritetabEntity storeup = storeupService.selectById(id);
        return R.ok().put("data", storeup);
    }

    /**
     * 前端详情
     */
    @IgnoreAuth
    @RequestMapping("/detail/{id}")
    public R detail(@PathVariable("id") Long id){
        FavoritetabEntity storeup = storeupService.selectById(id);
        return R.ok().put("data", storeup);
    }

    /**
     * 后端保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody FavoritetabEntity storeup, HttpServletRequest request){
        storeup.setId(new Date().getTime()+new Double(Math.floor(Math.random()*1000)).longValue());
        //ValidatorUtils.validateEntity(storeup);
        storeup.setUserid((Long)request.getSession().getAttribute("userId"));
        storeupService.insert(storeup);
        return R.ok();
    }

    /**
     * 前端保存
     */
    @RequestMapping("/add")
    public R add(@RequestBody FavoritetabEntity storeup, HttpServletRequest request){
        storeup.setId(new Date().getTime()+new Double(Math.floor(Math.random()*1000)).longValue());
        //ValidatorUtils.validateEntity(storeup);
        storeupService.insert(storeup);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @Transactional
    public R update(@RequestBody FavoritetabEntity storeup, HttpServletRequest request){
        //ValidatorUtils.validateEntity(storeup);
        storeupService.updateById(storeup);//全部更新
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
        storeupService.deleteBatchIds(Arrays.asList(ids));
        return R.ok();
    }
}
